package com.ruoyi.yuce.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.yuce.domain.KwhData;
import com.ruoyi.yuce.domain.pojo.CoBuJian;
import com.ruoyi.yuce.domain.pojo.CoBuJianLeiXing;
import com.ruoyi.yuce.domain.pojo.CoChangZhan;
import com.ruoyi.yuce.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 电度记录Controller
 */
@RestController
@RequestMapping("/ruoyi-yuce/kwhdata")
@DataSource(value = DataSourceType.SLAVELS)
public class KwhDataController extends BaseController
{
    @Autowired
    private IKwhDataService kwhDataService;

    @Autowired
    private IChangzhanService changzhanService;

    @Autowired
    private IBujianleixingService bujianleixingService;

    @Autowired
    private IFengjiService fengjiService;

    @Autowired
    private INibianqiService nibianqiService;

    /**
     * 查询遥测记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(KwhData kwhData) {
        startPage();
        Date date = Date.valueOf(kwhData.getCriqi());
        SimpleDateFormat sfb = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String biao="ls.kwhdata"+sfb.format(date);
        String time=sf.format(date);
        kwhData.setBriqi(biao);
        kwhData.setCriqi(time);
        List<KwhData> list = kwhDataService.selectKwhDataList(kwhData);
        return getDataTable(list);
    }

    /**
     * 查询部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getBuJian')")
    @GetMapping("/getBuJian")
    public List<CoBuJian> getBuJian(KwhData kwhData) {
        logger.info("getBuJian function ...");
        int n = kwhData.getBujianleixingid();
        List<CoBuJian> buJian = null;
        if (n== 49){
            buJian = fengjiService.getBuJian();
        }else if(n == 45){
            buJian = nibianqiService.getBuJian();
        }
        return buJian;
    }
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getBuJian1')")
    @GetMapping("/getBuJian1")
    public List<CoBuJian> getBuJian1(KwhData kwhData) {
        logger.info("getBuJian function ...");
        int n = kwhData.getBujianleixingid();

        List<CoBuJian> buJian = null;
        if (n== 49){
            buJian = fengjiService.getBuJian1(kwhData.getChangzhanid());
        }else if(n == 45){
            buJian = nibianqiService.getBuJian();
        }
        return buJian;
    }
    /**
     * 查询场站名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getChangZhanName')")
    @GetMapping("/getChangZhanName")
    public List<CoChangZhan> getChangZhanName()
    {
        logger.info("getChangZhanName function ...");
        List<CoChangZhan> changZhanName = changzhanService.getChangZhanName();
        return changZhanName;
    }

    /**
     * 查询部件类型名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getBuJianLeiXing')")
    @GetMapping("/getBuJianLeiXing")
    public List<CoBuJianLeiXing> getBuJianLeiXing()
    {
        logger.info("getBuJianLeiXing function ...");
        List<CoBuJianLeiXing> buJianLeiXing = bujianleixingService.getBuJianLeiXing();
        return buJianLeiXing;
    }

    /**
     * 查询风机部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getFengJi')")
    @GetMapping("/getFengJi")
    public List<CoBuJian> getFengJi() {
        logger.info("getFengJi function ...");
        List<CoBuJian> buJian = fengjiService.getBuJian();
        return buJian;
    }

    /**
     * 查询逆变器部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:getNiBianQi')")
    @GetMapping("/getNiBianQi")
    public List<CoBuJian> getNiBianQi() {
        logger.info("getNiBianQi function ...");
        List<CoBuJian> buJian = nibianqiService.getBuJian();
        return buJian;
    }


    /**
     * 导出遥测记录表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:kwhdata:export')")
    @Log(title = "遥测记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KwhData kwhData)
    {

        Date date = Date.valueOf(kwhData.getCriqi());
        SimpleDateFormat sfb = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String biao="ls.kwhdata"+sfb.format(date);
        String time=sf.format(date);
        kwhData.setBriqi(biao);
        kwhData.setCriqi(time);
        List<KwhData> list = kwhDataService.selectKwhDataList(kwhData);
        ExcelUtil<KwhData> util = new ExcelUtil<KwhData>(KwhData.class);
        util.exportExcel(response, list, "遥测记录表");
    }


}
